#include <stdio.h>
#include <string.h> 
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

int main()
{
	printf("服务器创建socket...\n");
	int sockfd = socket(AF_INET,SOCK_STREAM,0);
	if(0 > sockfd)
	{
		perror("socket");
		return -1;
	}

	printf("准备地址...\n");
	struct sockaddr_in addr = {};
	addr.sin_family = AF_INET;
	addr.sin_port = htons(7777);//端口号
	addr.sin_addr.s_addr = inet_addr("47.97.229.46");//47.97.229.46为服务器的公网ip地址
	socklen_t len = sizeof(addr);

	printf("连接服务器...\n");
	if(connect(sockfd,(struct sockaddr*)&addr,len))
	{
		perror("bind");
		return -1;
	}


	printf("等待客户端连接...\n");
	for(;;)
	{

			char buf[1024] = {};
				printf(">");
				gets(buf);
				write(sockfd,buf,strlen(buf)+1);
				if(0 == strcmp("quit",buf))
				{
					printf("通信结束！\n");
					break;
				}
				printf("read:");
				read(sockfd,buf,sizeof(buf));
				printf("%s\n",buf);
	}
	close(sockfd);

}

